layui.config({
    base: 'layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'user'], function() {
    var $ = layui.$,
        setter = layui.setter,
        admin = layui.admin,
        form = layui.form,
        router = layui.router(),
        search = router.search,
        condition = false;

    form.render();
    document.onkeyup = (e) => {
        if (e == 13) {
            $submit();
        }
    };
    let data = layui.data('Administrator');
    if (data.password) {
        form.val("login", {
            "username": data.username,
            "password": data.password,
            "remember": true
        });
    }

    //提交
    let $submit = form.on('submit(LAY-user-login-submit)', function(obj) {
        //请求登入接口
        admin.req({
            url: "backstage.action?methodName=login" //实际使用请改成服务端真实接口
            ,
            data: obj.field,
            type: "post",
            done: function(res) {
                switch (res.msg) {
                    case "登入成功":
                        var data = form.val("login");
                        if (data.remember) {
                            layui.data('Administrator', {
                                key: 'username',
                                value: obj.field.username
                            });
                            layui.data('Administrator', {
                                key: 'password',
                                value: obj.field.password
                            });
                        } else {
                            layui.data('Administrator', null)
                        };
                        layer.msg("登录成功，即将跳转",{icon: 1,time:1000*2});
                        layui.sessionData("management",{
                            key : "management",
                            value: res.management
                        })
                        setInterval(() => {
                            location.replace(res.index);
                        }, 1000 * 2);
                        window.location.replace("console.html");

                        break;
                    case "登入失败":
                        layer.msg('账号或密码错误请新登录', {
                            icon: 5
                        });
                }
            }
        });

    });
    $("#LAY-user-login-vercode").keyup(function(obj) {
        let $code = $("#LAY-user-login-vercode").val();
        if ($code != null && $code.length < 5) {
            $("#LAY-user-login-vercode").removeClass('layui-disabled');
        } else if ($code != null && $code.length == 5) {
            $.ajax({
                url: 'Verification.action?methodName=verify',
                type: 'post',
                dataType: "json",
                data: {
                    "code": $('#LAY-user-login-vercode').val()
                },
                success: function(res) {
                    if (res.code == "0") {
                        $('#submint-btn').removeClass('layui-btn-disabled').removeAttr('disabled', "true");
                        form.render();
                    } else {
                        layer.msg("验证码有误,请重新输入", {icon: 5})
                        $('#submint-btn').addClass('layui-btn-disabled').attr('disabled', "true");
                        form.render();
                    }
                }
            })
        } else if ($code != null && $code.length > 5){
            layer.msg("只能录入5个值", {icon: 2})
            // $("#LAY-user-login-vercode").addClass('layui-disabled').attr('readonly', 'readonly' );
            $("#LAY-user-login-vercode").val($code.substring(0,5))
            $('#submint-btn').addClass('layui-btn-disabled').attr('disabled', "true");
            form.render();
        }else {
            $('#submint-btn').addClass('layui-btn-disabled').attr('disabled', "true");
            form.render();
        }

    });
});